package com.ls.nexgo;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.widget.Toast;

import com.nexgo.oaf.apiv3.APIProxy;
import com.nexgo.oaf.apiv3.DeviceEngine;
import com.nexgo.oaf.apiv3.device.led.LEDDriver;
import com.nexgo.oaf.apiv3.device.led.LightModeEnum;
import com.nexgo.oaf.apiv3.device.printer.AlignEnum;
import com.nexgo.oaf.apiv3.device.printer.DotMatrixFontEnum;
import com.nexgo.oaf.apiv3.device.printer.FontEntity;
import com.nexgo.oaf.apiv3.device.printer.OnPrintListener;
import com.nexgo.oaf.apiv3.device.printer.Printer;

public class PrintNexgo {
    private DeviceEngine deviceEngine;
    private Printer printer;
    private LEDDriver ledDriver;
    private FontEntity fontNormal = new FontEntity(
            DotMatrixFontEnum.CH_SONG_24X24,
            DotMatrixFontEnum.ASC_SONG_12X24
    );

    public PrintNexgo(Context context) {
        try {
            deviceEngine = APIProxy.getDeviceEngine(context);
            ledDriver = deviceEngine.getLEDDriver();
            printer = deviceEngine.getPrinter();
            printer.initPrinter();
            printer.setLetterSpacing(0);
        } catch (UnsatisfiedLinkError e) {
            Toast.makeText(context, String.valueOf(e), Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(context, String.valueOf(e), Toast.LENGTH_LONG).show();
        }
    }

    public void setText(String text, AlignEnum align, Context context) {
        try {

            printer.appendPrnStr(text, fontNormal, align);
            printer.startPrint(true, new OnPrintListener() {
                @Override
                public void onPrintResult(int i) {
                    // skip.
                }
            });
        } catch (Exception e) {
            Toast.makeText(context, String.valueOf(e), Toast.LENGTH_LONG).show();
        }
    }

    public void setFeed(int feed, Context context) {
        try {

            printer.feedPaper(feed);
            printer.startPrint(true, new OnPrintListener() {
                @Override
                public void onPrintResult(int i) {
                    // skip.
                }
            });
        } catch (Exception e) {
            Toast.makeText(context, String.valueOf(e), Toast.LENGTH_LONG).show();
        }
    }

    public void setImage(Bitmap image, AlignEnum align, Context context) {
        try {

            printer.appendImage(image, align);
            printer.startPrint(true, new OnPrintListener() {
                @Override
                public void onPrintResult(int i) {
                    // skip.
                }
            });
        } catch (Exception e) {
            Toast.makeText(context, String.valueOf(e), Toast.LENGTH_LONG).show();
        }
    }

    public void setLED(LightModeEnum light, boolean enabled) {
        ledDriver.setLed(light, enabled);
    }
}
